import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseIntPipe,
} from '@nestjs/common';
import { MessagesService } from './messages.service';
import { SendMessageDto } from './dto/send-message.dto';
import { CreateNotificationDto } from './dto/create-notification.dto';
import { AuthGuard } from '../../common/guards/auth.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import type { JwtPayload } from '../auth/interfaces/jwt-payload.interface';
import { NotificationType } from './entities/notification.entity';

@Controller('messages')
@UseGuards(AuthGuard)
export class MessagesController {
  constructor(private readonly messagesService: MessagesService) {}

  // ==================== 私信消息相关 ====================

  /**
   * 发送消息
   */
  @Post('send')
  sendMessage(@Body() dto: SendMessageDto, @CurrentUser() user: JwtPayload) {
    return this.messagesService.sendMessage(dto, user.sub);
  }

  /**
   * 获取会话列表
   */
  @Get('conversations')
  getConversations(
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.messagesService.getConversations(
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 20,
    );
  }

  /**
   * 获取会话消息列表
   */
  @Get('conversations/:conversationId')
  getConversationMessages(
    @Param('conversationId') conversationId: string,
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.messagesService.getConversationMessages(
      conversationId,
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 50,
    );
  }

  /**
   * 标记消息为已读
   */
  @Post(':id/read')
  markMessageAsRead(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.messagesService.markMessageAsRead(id, user.sub);
  }

  /**
   * 标记会话所有消息为已读
   */
  @Post('conversations/:conversationId/read')
  markConversationAsRead(
    @Param('conversationId') conversationId: string,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.messagesService.markConversationAsRead(
      conversationId,
      user.sub,
    );
  }

  /**
   * 删除消息
   */
  @Delete(':id')
  deleteMessage(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.messagesService.deleteMessage(id, user.sub);
  }

  /**
   * 获取未读消息总数
   */
  @Get('unread/count')
  getUnreadMessagesCount(@CurrentUser() user: JwtPayload) {
    return this.messagesService.getUnreadMessagesCount(user.sub);
  }

  // ==================== 系统通知相关 ====================

  /**
   * 创建通知（通常由系统内部调用，也可以手动测试）
   */
  @Post('notifications')
  createNotification(@Body() dto: CreateNotificationDto) {
    return this.messagesService.createNotification(dto);
  }

  /**
   * 获取通知列表
   */
  @Get('notifications')
  getNotifications(
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
    @Query('type') type?: NotificationType,
  ) {
    return this.messagesService.getNotifications(
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 20,
      type,
    );
  }

  /**
   * 标记通知为已读
   */
  @Post('notifications/:id/read')
  markNotificationAsRead(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.messagesService.markNotificationAsRead(id, user.sub);
  }

  /**
   * 标记所有通知为已读
   */
  @Post('notifications/read-all')
  markAllNotificationsAsRead(@CurrentUser() user: JwtPayload) {
    return this.messagesService.markAllNotificationsAsRead(user.sub);
  }

  /**
   * 删除通知
   */
  @Delete('notifications/:id')
  deleteNotification(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.messagesService.deleteNotification(id, user.sub);
  }

  /**
   * 清空所有通知
   */
  @Delete('notifications/clear-all')
  clearAllNotifications(@CurrentUser() user: JwtPayload) {
    return this.messagesService.clearAllNotifications(user.sub);
  }

  /**
   * 获取未读通知总数
   */
  @Get('notifications/unread/count')
  getUnreadNotificationsCount(@CurrentUser() user: JwtPayload) {
    return this.messagesService.getUnreadNotificationsCount(user.sub);
  }
}
